import React from 'react';
import PropTypes from 'prop-types';
import { Router } from 'dva/router';
import App from './routes/app';
import Login from './routes/login';


const registerModel = (app, model) => {
	if (!(app._models.filter(m => m.namespace === model.namespace).length === 1)) {
		app.model(model);
	}
};

let pathChange = false;

const Routers = ({ history, app }) => {
	const routes = [
		{
			path: '/login',
			component: Login
		},
		{
			path: '/',
			component: App,
			getIndexRoute(nextState, cb) {
				pathChange = false;
				require.ensure([], require => {
					registerModel(app, require('./models/productList'));
					if (!pathChange) {
						cb(null, require('./routes/productList'));
					}
				}, 'productList');
			},
			childRoutes: [
				{
					path: 'sysUser',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/sysRole'));
							cb(null, require('./routes/sysUser'));
						}, 'sysUser');
					},
				},
				{
					path: 'sysRole',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/sysRole'));
							cb(null, require('./routes/sysRole'));
						}, 'sysRole');
					},
				},
				{
					path: 'sysPermission',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/sysPermission'));
							cb(null, require('./routes/sysPermission'));
						}, 'sysPermission');
					},
				},
				{
					path: 'sysConfig',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/sysConfig'));
							cb(null, require('./routes/sysConfig'));
						}, 'sysConfig');
					},
				},
				{
					path: 'productType',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/productType'));
							cb(null, require('./routes/productType'));
						}, 'productType');
					},
				},
				{
					path: 'productType/:id',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							cb(null, require('./routes/productType/detail'));// 上面已经包含
						}, 'productType');
					},
				},
				{
					path: 'productList',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							cb(null, require('./routes/productList'));
						}, 'productList');
					},
				},
				{
					path: 'productList/:id',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							registerModel(app, require('./models/device'));

							cb(null, require('./routes/productList/detail'));
						}, 'productList');
					},
				},
        {
          path: 'setting',
          getComponent(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/app'));
              cb(null, require('./routes/setting'));
            }, 'setting');
          },
        },
        {
          path: 'scanQrcode',
          getComponent(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/scanQrcode'));
              cb(null, require('./routes/scanQrcode'));
            }, 'scanQrcode');
          },
        },
        {
          path: 'testDevice',
          getComponent(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testDevice'));
              cb(null, require('./routes/testDevice'));
            }, 'testDevice');
          },
        },
        {
          path: 'testResult',
          getComponent(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/testResult'));
              cb(null, require('./routes/testResult'));
            }, 'testResult');
          },
        },
				{
					path: '*',
					getComponent(nextState, cb) {
						require.ensure([], require => {
							cb(null, require('./routes/error'));
						}, 'error');
					},
				},
			],
		}
	];

	return <Router history={history} routes={routes} onUpdate={() => pathChange = true} />;
};

Routers.propTypes = {
	history: PropTypes.object,
	app: PropTypes.object,
};

export default Routers;
